<?php


use App\Facades\SnowFlakeFacade;
use App\Models\Common\Channel\Channel;
use App\Trip\Enums\OrderBusiness;
use App\Trip\Enums\TripTypes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

if (!function_exists('app_is_init')) {
    /**
     * 项目是否初始化
     *
     * @return bool
     */
    function app_is_init(): bool
    {
        $cache_key = "func:application:init";
        $lock_file = storage_path('app/init.lock');
        if (Cache::has($cache_key)) {
            return true;
        }
        if (!file_exists($lock_file)) {
            return false;
        } else {
            Cache::forever($cache_key, true);
        }
        return true;
    }

}

if (!function_exists('app_initialized')) {
    /**
     * 项目完成初始化
     *
     * @return bool
     */
    function app_initialized(): bool
    {
        if (app_is_init()) {
            return true;
        }
        $cache_key = "func:application:init";
        $lock_file = storage_path('app/init.lock');
        file_put_contents($lock_file, true);
        Cache::forever($cache_key, true);
        return true;
    }

}

if (!function_exists('trans2uy')) {
    /**
     * @param string $string
     * @return string
     */
    function trans2uy(string $string): string
    {
        return __($string, [], 'uy');
    }

}


if (!function_exists('official_station_version')) {

    /**
     * @throws Exception
     */
    function official_station_version(): string
    {
        if (Cache::has("Train:Official:Station:Version")) {
            return Cache::get("Train:Official:Station:Version");
        }
        if (file_exists(public_path('Train/12306/station_version'))) {
            return file_get_contents(public_path('Train/12306/station_version'));
        }

        return '';
    }

}

if (!function_exists('random_number')) {

    /**
     * 获取长度为 length 的随机数
     * @throws Exception
     */
    function random_number(int $length = 20): string|int
    {
        $numbers = [];
        for ($i = 0; $i < $length; $i++) {
            $start     = $i == 0 ? 1 : 0;
            $numbers[] = random_int($start, 9);
        }
        return implode('', $numbers);
    }

}


if (!function_exists('ticket_failed_refund_title')) {

    /**
     * 乘客出票失败退款标题
     * @param string $passenger_name
     * @return string
     */
    function ticket_failed_refund_title(string $passenger_name = ""): string
    {
        return Str::replace(['{NAME}', '{Name}'], [$passenger_name, $passenger_name], __("refund/order_or_ticket_refund_title.乘客{NAME}出票失败自动退款"));
    }

}


if (!function_exists('Minutes2Seconds')) {
    function Minutes2Seconds(int $minutes = 1): int
    {
        return 60 * $minutes;
    }
}

if (!function_exists('Hours2Seconds')) {
    function Hours2Seconds(int $hours = 1): int
    {
        return Minutes2Seconds() * 60 * $hours;
    }
}

if (!function_exists('Days2Seconds')) {
    function Days2Seconds(int $day = 1): int
    {
        return $day * 24 * Hours2Seconds();
    }
}


if (!function_exists('get_weather_type')) {
    /**
     * 根据天气名称返回类型
     *
     * @param string $weather
     *
     * @return int
     */
    function get_weather_type(string $weather): int
    {
        $types = config('trip.weather.types');
        return $types[$weather] ?? 0;
    }
}

if (!function_exists('get_weather_icon')) {
    /**
     * 根据天气名称返回图标
     *
     * @param string $weather
     *
     * @return string
     */
    function get_weather_icon(string $weather): string
    {
        $type = get_weather_type($weather);
        if (0 == $type) {
            return '';
        }

        $url = "https://trip-dove.oss-cn-beijing.aliyuncs.com/weather/%d.png";
        return sprintf($url, $type);
    }
}

if (!function_exists('tail_phone_number')) {
    /**
     * 手机号 后N位
     * @param string $phone
     * @param int    $length
     * @return string
     */
    function tail_phone_number(string $phone, int $length = 4): string
    {
        $start = 11 - $length;
        return Str::substr($phone, $start, $length);
    }
}


if (!function_exists('generate_order_id')) {
    /**
     * @param TripTypes     $trip_type
     * @param OrderBusiness $business
     * @return string
     */
    function generate_order_id(TripTypes $trip_type, OrderBusiness $business): string
    {
        $t = $trip_type->order_flag();
        $b = $business->business_flag();
        return $t.$b.SnowFlakeFacade::id();
    }
}


if (!function_exists('generate_request_id')) {
    /**
     * 请求trace_id
     * @return string
     */
    function generate_request_id(): string
    {
        return SnowFlakeFacade::id();
    }
}


if (!function_exists('remove_city_name_shi')) {
    /**
     * 移除城市名称里的最后一个 '市'
     * @param string $city_name
     * @return string
     */
    function remove_city_name_shi(string $city_name): string
    {
        if (Str::endsWith($city_name, "市")) {
            $city_name = Str::replaceLast("市", '', $city_name);
        }
        return $city_name;
    }
}


if (!function_exists('isUseCity')) {
    /**
     *   地区名称确定是否使用城市
     */
    function isUseCity(string $area_name): int
    {
        $isUseCity = false;
        if (Str::endsWith($area_name, '区') && !Str::contains($area_name, '地区')) {
            $isUseCity = true;
        }
        return $isUseCity;
    }
}

if (!function_exists('channel')) {
    /**
     * 客户端渠道
     * @return Channel|null
     */
    function channel(): ?Channel
    {
        try {
            return app()->get('trip.channel') ?: null;
        } catch (Exception $e) {
            return null;
        }
    }
}